package com.gx.util.dbutil.jdbcdao;

import com.alibaba.druid.pool.DruidDataSourceFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.sql.DataSource;
import java.io.*;
import java.util.Properties;

/**
 * Created by zq on 2017/3/28.
 */
public class DataSourceHepler {
    private Logger log = LoggerFactory.getLogger(DataSourceHepler.class);

//    private static DataSourceHepler dataSourceHepler = null;

    private DataSource dataSource;
    private String confile = "db.properties";
    private Properties p = null;

//    private DataSourceHepler(){
//    }



//    public static DataSourceHepler getInstance(){
//        if(dataSourceHepler ==null){
//            syncInit();
//        }
//        return dataSourceHepler;
//    }
//    private static synchronized void syncInit() {
//        if (dataSourceHepler == null) {
//            dataSourceHepler = new DataSourceHepler();
//        }
//    }

    public DataSource getDataSource() throws Exception {
        if(dataSource==null) {
            dataSource = DruidDataSourceFactory.createDataSource(p);
        }
        return dataSource;
    }


    public DataSourceHepler bulidProp(Properties p) {
        this.p = p;
        return this;
    }

    public DataSourceHepler bulidProp(String confile) throws IOException {
        p = new Properties();
        InputStream inputStream = null;
        ClassLoader loader = DataSourceHepler.class.getClassLoader();
        inputStream = loader.getResourceAsStream(confile);
        p.load(inputStream);
        if (inputStream != null) {
            inputStream.close();
        }
        return this;
    }
}
